package cn.fl.project.service.impl;

import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.logger.eum.LogOperEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.service.IPrjLnkCompCrLimService;
import cn.fl.project.transform.PrjLnkCompCrLimVOTransform;
import cn.fl.project.vo.PrjLnkCompCrLimVO;
import fl.constant.facade.framework.constant.BusinessIdConstant;
import fl.flow.facade.flow.constant.FlowConstant;
import fl.flow.facade.flow.dto.FlowStartParamsDTO;
import fl.spl.facade.framework.constant.SplSystemConstant;
import fl.spl.facade.lnkcomp.api.IPrjLnkCompCrLimDMLApiService;
import fl.spl.facade.lnkcomp.api.IPrjLnkCompCrLimQueryApiService;
import fl.spl.facade.lnkcomp.condition.PrjLnkCompCrLimQC;
import fl.spl.facade.lnkcomp.constant.PrjLnkCompRelInfoConstant;
import fl.spl.facade.lnkcomp.dto.PrjLnkCompCrLimDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 挂靠公司授信额度 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class PrjLnkCompCrLimServiceImpl implements IPrjLnkCompCrLimService {

	@Autowired
	private IPrjLnkCompCrLimDMLApiService prjLnkCompCrLimDMLApiService ;

	@Autowired
	private IPrjLnkCompCrLimQueryApiService prjLnkCompCrLimQueryApiService ;

	@Override
	public PrjLnkCompCrLimVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.newWebLogger("主键参数不能为空!").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SQOT_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			return null;
		}
		//TODO 业务逻辑
		PrjLnkCompCrLimDTO dto = this.prjLnkCompCrLimQueryApiService.selectPrjLnkCompCrLimDTOById(id);
		return PrjLnkCompCrLimVOTransform.toVO(dto);
	}

	@Override
	public Long add(PrjLnkCompCrLimVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.newWebLogger("新增对象不能为空!").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SQOT_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			return null;
		}
		return this.prjLnkCompCrLimDMLApiService.addPrjLnkCompCrLimDTO(PrjLnkCompCrLimVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			return false;
		}
		//return this.prjLnkCompCrLimApiService.removeById(id);
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
	@Override
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
			BaseLogUtils.newWebLogger("删除主键参数集合不能为空!").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SQOT_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			return false;
		};
		return this.prjLnkCompCrLimDMLApiService.removeByIds(ids);
    }

	@Override
	public boolean modify(PrjLnkCompCrLimVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.newWebLogger("修改对象参数不能为空!").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SQOT_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			return false;
		}
		return this.prjLnkCompCrLimDMLApiService.modifyPrjLnkCompCrLimDTO(PrjLnkCompCrLimVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page){
			page = new Pager();
		}
		return this.prjLnkCompCrLimQueryApiService.selectLisBytPage(page);
	}

	@Override
	public List<PrjLnkCompCrLimVO> selectList(PrjLnkCompCrLimQC qc) throws BaseException {
		return PrjLnkCompCrLimVOTransform.toVOList(prjLnkCompCrLimQueryApiService.searchListByQC(qc));
	}

	@Override
	public DataResultVO addAndApply(PrjLnkCompCrLimVO entity) throws BaseException {
		DataResultVO resultVO = new  DataResultVO();
		FlowStartParamsDTO flowStartParamsDTO = this.getFlowStartParams(FlowConstant.FLOW_BUSINESS_TYP_LNK,FlowConstant.FLOW_MODEL_TYP_LNK_2);
		PrjLnkCompCrLimDTO dto = PrjLnkCompCrLimVOTransform.toDTO(entity);
		dto.setFlowStartParamsDTO(flowStartParamsDTO);
		DataResultDTO resultdto = this.prjLnkCompCrLimDMLApiService.addPrjLnkCompCrLimAndApplyFlowStart(dto);
		resultVO.setData(resultdto.getData());
		resultVO.setInfo(resultdto.getInfo());
		return resultVO;
	}
	

	/******************************************************
	 * 获取发起流程对象
	 * 
	 * @param businessType
	 *            :业务类型(FlowConstant.FLOW_BUSINESS_TYP_SETTLE)
	 * @param modelTyp
	 *            :流程业务分类 正常结清:FlowConstant.FLOW_MODEL_TYP_SETTLE_NORMAL
	 *            提前结清:FlowConstant.FLOW_MODEL_TYP_SETTLE_ADVANCE
	 *            强制结清:FlowConstant.FLOW_MODEL_TYP_SETTLE_FORCE
	 * @return
	 * @throws BaseException
	 *****************************************************/
	private FlowStartParamsDTO getFlowStartParams(String businessType,
			String modelTyp) throws BaseException {
		if (StringUtils.isEmpty(modelTyp)) {
			throw new BaseException(
					BaseExceptionConstant.COMMON_0001, "流程业务分类不能为空");
		}
		FlowStartParamsDTO flowStartParamsDTO = new FlowStartParamsDTO();
		// 流程定义KEY和流程定义ID不定义 流程发起的时候会根据模块类型查找唯一流程去发起
		// flowStartParamsDTO.setProcDefKey("");
		// flowStartParamsDTO.setProcDefId("");
		flowStartParamsDTO.setAudStsCd(FlowConstant.FLOW_CHECK_STATE_IN);
		flowStartParamsDTO.setWordsInTheTable(PrjLnkCompRelInfoConstant.FLOW_S_H_LNK);
		flowStartParamsDTO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
		flowStartParamsDTO.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
		flowStartParamsDTO.setSysCd(CurrentThreadContext.getCurrentSysCd());
		flowStartParamsDTO.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
		flowStartParamsDTO.setInitiator(String.valueOf(CurrentThreadContext
				.getCurrentUserId()));
		flowStartParamsDTO.setInitiatorNm(CurrentThreadContext
				.getCurrentUserName());
		flowStartParamsDTO
				.setProcPriority(FlowConstant.FLOW_PRIORITY_EMERGENCY);// 优先级,可以在界面定义
		flowStartParamsDTO.setBusinessType(businessType);// TODO 确认用不用
		flowStartParamsDTO.setModelTyp(modelTyp);// 需要和流程配置中的模块类型匹配
		return flowStartParamsDTO;
	}

}
